package com.fire.controller.impl;

import com.fire.controller.IStudentController;
import com.fire.domain.Student;
import com.fire.factory.Factory;
import com.fire.service.IStudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class StudentControllerImpl implements IStudentController {
    private IStudentService studentService = Factory.getStudentServiceImpl();
    private Scanner sc = new Scanner(System.in);
    private static final Logger LOGGER= LoggerFactory.getLogger(TeacherControllerImpl.class);

    @Override
    public void start() throws IOException {
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生   5.导入数据  6.导出数据  7.退出");
            String s = sc.next();
            switch (s) {
                case "1":
                    //System.out.println("新增学生");
                    add();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    delete();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    update();
                    break;
                case "4":
                    //System.out.println("查看学生");

                    lo:
                    while (true) {
                        System.out.println("请您选择 1.模糊查找 2.全部查找 3.退出此界面 ");
                        String choice = sc.next();
                        switch (choice) {
                            case "1":
                                chakan();
                                break;
                            case "2":
                                findAll();
                                break;
                            case "3":
                                System.out.println("退出");
                                break lo;
                            default:
                                System.out.println("录入有误,请重新输入");
                                break;
                        }
                    }
                    //                case "5":
////                    System.out.println("报表统计");
//                    find();
                    break;
                case "5":
                    System.out.println("导入数据");
                    dauru();
                    System.out.println("是否查看报表统计y/n");
                    String s1 = sc.next();
                    if ("y".equals(s1)) {
                        find();
                    }
                    break;
                case "6":
//                    System.out.println("导出数据");
                    daochu();
                    System.out.println("导出成功");
                    break;
                case "7":
                    System.out.println("感谢使用学生系统");
                    return;
                default:
                    System.out.println("请输入正确的选择");
            }
        }
    }

    private void chakan() {
        lo:
        while (true) {
            System.out.println("请您选择 1.姓名查找 2.手机查找 3.退出此界面");
            String s2 = sc.next();
            switch (s2) {
                case "1":
                    System.out.println("姓名查找");
                    String name = sc.next();
                    searchName(name);
                    break;
                case "2":
                    System.out.println("手机查找");
                    String phone = sc.next();
                    searchPhone(phone);
                    break;
                case "3":
                    System.out.println("退出系统");
                    break lo;
                case "4":
                    System.out.println("请重新录入");
                    break;
            }
        }
    }

    public void daochu() {
        ArrayList<Student> list = studentService.findAll();
        if (list == null || list.size() == 0) {
            System.out.println("没有数据，请先添加");
        }
        studentService.daochu();
    }

    @Override
    public void dauru() {
        long start = System.currentTimeMillis();
        studentService.daoru();
        long end = System.currentTimeMillis();
        long l = end - start;
        System.out.println("导入时间为"+l);
    }

    @Override
    public void searchName(String name) {
        ArrayList<Student> students = studentService.searchName(name);
        if (students.size()==0){
            System.out.println("没有该姓的学生");
        }else {
        for (Student student : students) {
            System.out.println(student);
        }
    }}

    @Override
    public void searchPhone(String phone) {
        ArrayList<Student> students = studentService.searchPhone(phone);
        if (students.size()==0){
            System.out.println("没有该手机号的学生");
        }else{
        for (Student student : students) {
            System.out.println(student);
        }}
    }


    public void getGroup() throws IOException {
        HashMap<Integer, ArrayList<Student>> map = studentService.getGroup();
        Set<Map.Entry<Integer, ArrayList<Student>>> entries = map.entrySet();
//        for (Map.Entry<Integer, ArrayList<Student>> entry : entries) {
//            Integer key = entry.getKey();
//            ArrayList<Student> value = entry.getValue();
////            int size1 = value.size();
//            System.out.println(key+"组有"+entry.getValue().size()+"人");
        map.keySet().stream().forEach(s -> System.out.println(s + "组有" + map.get(s).size() + "人"));
    }


    public void find() throws IOException {
        ArrayList<Integer> list = studentService.getfind();
        System.out.println("男生有" + list.get(0) + "人");
        System.out.println("女生有" + list.get(1) + "人");
        System.out.println("18岁的学生有" + list.get(2) + "人");
        System.out.println("19岁的学生有" + list.get(3) + "人");
        System.out.println("20岁的学生有" + list.get(4) + "人");
        System.out.println("班级总人数有" + list.get(5) + "人");
        System.out.println("班级平均年龄为" + list.get(6) + "岁");
        getGroup();
    }
    @Override
    public void add() {
        String id = null;
        while (true) {
            System.out.println("请输入学号");
            id = sc.next();
            boolean exist = studentService.isExist(id);
            if (exist) {
                System.out.println("已经存在该学号,请重新输入");
            } else {
                break;
            }
        }
        Student student = getStudentInfo(id);
        studentService.add(student);
        System.out.println("添加成功");
    }

    @Override
    public void delete() throws IOException {
        ArrayList<Student> list = studentService.findAll();
        if (list == null || list.size() == 0) {
            System.out.println("没有数据,请先添加");
            return;
        }
        String id = getStudentId();
        studentService.delete(id);
        System.out.println("删除成功");

    }

    private String getStudentId() {
        String id = null;
        while (true) {
            System.out.println("请输入学号");
            id = sc.next();
            boolean exist = studentService.isExist(id);
            if (exist) {
                break;
            } else {
                System.out.println("已经存在该学号,请重新输入");
            }
        }
        return id;
    }

    @Override
    public void update() throws IOException {
        ArrayList<Student> list = studentService.findAll();
        if (list == null || list.size() == 0) {
            System.out.println("没有数据,请先添加");
            return;
        }
        String id = getStudentId();
        Student student = getStudentInfo(id);
        studentService.update(id, student);
        System.out.println("修改成功");
    }


    @Override
    public void findAll() {
        ArrayList<Student> list = studentService.findAll();
        //判断非空
        if (list == null || list.size() == 0) {
            System.out.println("没有数据,请先添加");
            //return;
        }
        //System.out.println(list);
        for (Student student : list) {
            System.out.println(student);
        }
    }

    private Student getStudentInfo(String id) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入性别");
        String gender = null;
        while (true) {
            System.out.println("请输入性别:");
            gender = sc.next();
            if (!("男".equals(gender)) && !("女".equals(gender))) {
                System.out.println("请正确输入性别!");
            } else {
                break;
            }
        }
        System.out.println("请输入生日(按照yyyy年MM月dd日的格式)");
        String birthday = sc.next();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        LocalDate date = LocalDate.parse(birthday, formatter);
        //System.out.println(date);
        //String[] split = date.split("-");
        //LocalDate birthday = LocalDate.of(Integer.parseInt(split[0]),Integer.parseInt(split[1]),Integer.parseInt(split[2]));
        System.out.println("请输入电话");
        String phoneNum = sc.next();
//        System.out.println("请输入年龄");
        Period p = Period.between(date, LocalDate.now());
        int age = p.getYears();
        System.out.println("请输入组号");
        int group = sc.nextInt();
        System.out.println("请输入微信号");
        String weChatNum = sc.next();
        return new Student(id, name, gender, date, phoneNum, age, group, weChatNum);
    }
}





